//
// Created by jojoli on 2024/8/10.
//

#ifndef RATES_THREADSAFEMAP_H
#define RATES_THREADSAFEMAP_H

#include <mutex>
#include <map>

template<typename Key, typename Value>
class ThreadSafeMap {
public:
    void insert(const Key& key, const Value& value) {
        std::lock_guard<std::mutex> lock(m_mutex);
        m_map.insert({key, value});
    }

    Value& at(const Key& key) {
        std::lock_guard<std::mutex> lock(m_mutex);
        return m_map.at(key);
    }

    bool contains(const Key& key) {
        std::lock_guard<std::mutex> lock(m_mutex);
        return m_map.find(key) != m_map.end();
    }

    void remove(const Key& key) {
        std::lock_guard<std::mutex> lock(m_mutex);
        m_map.erase(key);
    }

    // 其他方法...
private:
    std::map<Key, Value> m_map;
    std::mutex m_mutex;
};


#endif //RATES_THREADSAFEMAP_H
